<?php
class NoticeAction extends CommonAction {
    /**
     * 添加过滤条件
     * 设置失效时间大于当前时间
     */
	 public function _filter(&$map)
	{
		$title=$_REQUEST['title'];
		if(!empty($title))
		{
			$map['title']=array('like',"%".$title."%");
		}
		//$map['deadtime']=array('egt',date("Y-m-d",time()));//选择在失效的日期之前的信息
		$map['deadtime']=array(array('exp','is null'),array('egt',date("Y-m-d",time())),'or');//添加的动态显示出来以及选择在失效日期之前的信息
		$this->assign("title",$title);
	
	} 
	/**
	 * 重写插入方法
	 * 设置rank字段是自动增长的
	 * 
	 */
	public function insert()
	{
	          $notice=M('Notice');
 	          if($notice->create()) 
 	          {
 	          	//设置顺序的值
 	         	$id=$notice->max('rank');
 	          	$notice->rank=$id+1;
 	          	$data['title']=$_REQUEST['title'];
 	          	$data['author']=$_REQUEST['author'];
 	          	$data['pubdate']=date("Y-m-d",time());
 	          	$data['deadtime']=$_REQUEST['deadtime'];
 	          	$data['status']=$_REQUEST['status'];
 	          	$data['content']=$_REQUEST['content'];
 	          	$data['userid']=$_REQUEST['userid'];
	            $result = $notice->add($data);
 	            if($result) 
 	            {
 	            	$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
 	            	$this->insertLog('添加','操作公告管理编号为'.$result.'的数据成功');
	               $this->ajaxReturn($result,"信息添加成功",'200');  
 	             }else
 	             {
 	             	$this->insertLog('添加','操作公告管理编号为'.$result.'的数据失败');
 	                $this->ajaxReturn(0,'信息添加失败！',300);
 	              } 	        
 	          }
 	          else
 	            {
	              $this->error($notice->getError());
	            }  
		
		
	}
	
	
	public function edit()
	{
		$id=$_REQUEST['id'];
		$model=M('Notice');
		$list=$model->where('id='.$id)->find();
		$userid=$list['userid'];
		$uid = $_SESSION [C ( 'USER_AUTH_KEY' )];
		$this->assign('vo',$list);
		if($userid!=$uid)
		{
			$this->display('read');
		}
		else
		 {
		 	$this->display();
		 }
		
		
	}
	public function addTrends(){
		$this->display();
	}
	public function insertTrends(){
		$name=$this->getActionName();
		$model=D($name);
		if(false=== $model->create()){
			$this->ajaxReturn(1,$model->getError(),300);
		}
		$info = $this->getType($name);
		//保存当前数据对象
		$list=$model->add();
		if($list!== false){
			$this->assign('jumpUrl',Cookie::get(' currentUrl '));
			$this->insertLog('添加新动态','操作'.$info.'编号为'.$list.'的数据成功');
			$this->ajaxReturn (1,'新增成功!',200);
		}else {
			//失败提示
			$this->insertLog('添加新动态','操作'.$info.'编号为'.$list.'的数据失败');
			$this->ajaxReturn (1,'新增失败!',300);
		}
	}
		 
}
?>